package com.example.ex.Controller;

import com.example.ex.entity.Message;
import com.example.ex.entity.Question;
import com.example.ex.mapper.ChatMessageMapper;
import com.example.ex.mapper.LoginUserMapper;
import com.example.ex.pojo.ChatMessage;
import com.example.ex.pojo.LoginUser;
import com.example.ex.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.util.Base64;
import java.util.List;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.user;

@RestController
public class LoginController {

    @Autowired
    LoginUserMapper loginUserMapper;



//    @RequestMapping("/toLogin")
//    public Result tologin(@RequestParam("user") String user, @RequestParam("pwd") String pwd, HttpSession session) {
//        Result result = new Result();
//        if (user.equals("张三") && pwd.equals("123")) {
//            result.setFlag(true);
//            session.setAttribute("user", user);
//        } else if (user.equals("李四") && pwd.equals("123")) {
//            result.setFlag(true);
//            session.setAttribute("user", user);
//        } else if (user.equals("123") && pwd.equals("123")) {
//            result.setFlag(true);
//            session.setAttribute("user", user);
//        } else if (user.equals("王五") && pwd.equals("123")) {
//            result.setFlag(true);
//            session.setAttribute("user", user);
//        } else {
//            result.setFlag(false);
//            result.setMessage("登录失败");
//        }
//        return result;
//    }

    @RequestMapping ("/toLogin")
    public Result tologin(@RequestParam("user") String user, @RequestParam("pwd") String pwd, HttpSession session){
        Result result = new Result();
       /* if(loginUserMapper == null) System.out.println("mapper is null");
        else System.out.println("mapper is not null");
        if(chatMessageMapper == null) System.out.println("chatMessageMapper is null");
        else System.out.println("chatMessageMapper is not null");*/
        System.out.println("uname=" + user + "pwd" + pwd);
        if (user.equals("") || pwd.equals("")) {
            result.setFlag(false);
            result.setMessage("用户名或密码不能为空");
//        }
        } else {
            if (loginUserMapper.login(user, pwd) != null) {
                result.setFlag(true);
                result.setMessage("登录成功");
                session.setAttribute("user",user);

            } else {
                result.setFlag(false);
                result.setMessage("密码错误或用户不存在");
            }
        }
        return result;
    }

//        @RequestMapping("/toRegister")
//    @RequestMapping("/toRegister")
//    public Result toRegister(@RequestParam("user") String user, @RequestParam("pwd") String pwd) {
//        Result result = new Result();
//        System.out.println("uname=" + user + "pwd" + pwd);
//        if (user.equals("") || pwd.equals("")) {
//            result.setFlag(false);
//            result.setMessage("用户名或密码不能为空");
////        }
//        } else {
//            if (loginUserMapper.sel_name(user) == null) {
//                result.setFlag(true);
////                result.setMessage("注册成功");
//                result.setMessage(loginUserMapper.insert(new LoginUser(user,pwd)) > 0 ? "注册成功！" : "注册失败！");
//
//            } else {
//                result.setFlag(false);
//                result.setMessage("这个用户名已被注册！");
//            }
//        }
//        return result;
//    }


    @RequestMapping ("/toRegister")
    public Result register(@RequestParam("user") String user,
                           @RequestParam("pwd") String pwd,
                           @RequestParam("question") String question,
                           @RequestParam("answer") String answer,
                           @RequestParam("avatar") MultipartFile avatar) throws IOException {
        // 处理注册逻辑
        String base64 = null;
        try {
            byte[] bytes = avatar.getBytes();
            base64 = Base64.getEncoder().encodeToString(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }


    Result result = new Result();
        System.out.println("uname=" + user + "pwd" + pwd + "question" + question + "answer" + answer);
        if (user.equals("") || pwd.equals("") || question.equals("") || answer.equals("")) {
        result.setFlag(false);
        result.setMessage("用户名或密码或密保问题及答案不能为空");
//        }
    } else {
        if (loginUserMapper.sel_name(user) == null) {
            result.setFlag(true);
//                result.setMessage("注册成功");
            result.setMessage(loginUserMapper.insert(new LoginUser(user,pwd,question,answer,base64)) > 0 ? "注册成功！" : "注册失败！");

        } else {
            result.setFlag(false);
            result.setMessage("这个用户名已被注册！");
        }
    }
        return result;
        // 保存用户信息
    }


    @RequestMapping ("/changepwd")
    public Question changepwd(@RequestParam("user") String user) {
        Question question = new Question();
        if (loginUserMapper.sel_name(user) != null) {
            LoginUser loginUser = loginUserMapper.sel_name(user);
            question.setQuestion(loginUser.getQuestion());
            question.setAnswer(loginUser.getAnswer());
            question.setUser(loginUser.getUsername());
        }else question.setQuestion(null);
        return question;
    }



    @RequestMapping ("/questionok")
    public boolean questionok(@RequestParam("username") String username,@RequestParam("question") String question,@RequestParam("answer") String answer) {
        return loginUserMapper.questionok(username,question,answer) != null;
    }


    @RequestMapping("/change")
    public boolean change(@RequestParam("username") String username,@RequestParam("pwd1") String pwd1,@RequestParam("pwd2") String pwd2){
        if(pwd1 == pwd2)return false;
        else {
            loginUserMapper.updatePassword(username,pwd1);
            return true;
        }
    }

    @RequestMapping("/getAvatar")
    public String getAvatar(@RequestParam("username") String username)
    {
        String str = null;
        if(loginUserMapper.getAvatar(username) != null)str = loginUserMapper.getAvatar(username);

        System.out.println(str);
        return str;
    }
        @RequestMapping("/getUsername")
        public String getUsername (HttpSession session){
            return (String) session.getAttribute("user");
        }

 }